package com.czy.owner.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by hep-rz on 2017/8/28.
 * 简述
 */

public class StringUtils {
    public static String userNameHandling(String string) {
        if (string == null || string.isEmpty())
            return "";
        if (string.length() <= 7)
            return string;
        StringBuilder sb = new StringBuilder();
        sb.append(string.substring(0, 3));
        for (int i = 0; i < string.length() - 7; i++) {
            sb.append("*");
        }
        sb.append(string.substring(string.length() - 4, string.length()));
        return sb.toString();

    }

    public static String getNum(String string) {
        StringBuilder sb = new StringBuilder();
        if (isEmpty(string)) {
            return "000";
        } else if (string.length() == 1) {
            sb.append("00");
            return sb.append(string).toString();
        } else if (string.length() == 2) {
            sb.append("0");
            return sb.append(string).toString();
        } else {
            return string;
        }
    }

    public static boolean isEmpty(String string) {
        if (string == null || "".equals(string) || "null".equals(string)) {
            return true;
        }
        return false;
    }

    public static String getString(String string) {
        return string != null ? string : "";
    }

    public static String getString(String string, String defaultString) {
        if ("".equals(string))
            return defaultString;
        return string != null ? string : defaultString;
    }

    public static String getUTF8XMLString(String xml) {
        // A StringBuffer Object
        StringBuffer sb = new StringBuffer();
        sb.append(xml);
        String xmString = "";
        String xmlUTF8 = "";
        try {
            xmString = new String(sb.toString().getBytes("UTF-8"));
            xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");
            System.out.println("utf-8 编码：" + xmlUTF8);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // return to String Formed
        return xmlUTF8;
    }

    public static String getUTF(String string) {
        String encodedSign = null;
        try {
            encodedSign = URLEncoder.encode(string, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodedSign;
    }

    /**
     * Utf8URL编码
     *
     * @param
     * @return
     */
    public static String Utf8URLencode(String text) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c >= 0 && c <= 255) {
                result.append(c);
            } else {
                byte[] b = new byte[0];
                try {
                    b = Character.toString(c).getBytes("UTF-8");
                } catch (Exception ex) {
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0) k += 256;
                    result.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return result.toString();
    }
}
